
void do_write_at24cxx()
{
    unsigned int addr;
    unsigned char str[100];
    int err;

    printf("Enter the addr of sector to write: ");
    addr = get_uint();

    //判断地址
    if(addr > 256){
	printf("address > 256 , err! \r\n");
	return ;
    }

    printf("Enter the string to write: ");
    gets(str);

    printf("writing ... \n\r");
    err = at24cxx_write(addr,str,strlen(str) + 1);
    printf("at24cxx_write ret = %d\r\n",err);
}


void do_read_at24cxx()
{
    unsigned int addr;
    int i, j;
    unsigned char c;
    unsigned char data[100];
    unsigned char str[16];
    int len;
    int err;
    int cnt = 0;

    printf("Enter the addr of sector to read: ");
    addr = get_uint();

    //判断地址
    if(addr > 256){
	printf("address > 256 , err! \r\n");
	return;
    }

    /* 获得长度 */
    printf("Enter the length to read: "); 
    len = get_int();

    err = at24cxx_read(addr, data, len);
    printf("at24cxx_read ret = %d\n\r", err);

    printf("Data : \n\r");
    /* 长度固定为64 */
    for (i = 0; i < 4; i++)
    {
	/* 每行打印16个数据 */
	for (j = 0; j < 16; j++)
	{
	    /* 先打印数值 */
	    c = data[cnt++];
	    str[j] = c;
	    printf("%02x ", c); 
	}

	printf("   ; "); 

	for (j = 0; j < 16; j++)
	{   
	    /* 后打印字符 */
	    if (str[j] < 0x20 || str[j] > 0x7e)  /* 不可视字符 */
		putchar('.');
	    else
		putchar(str[j]);
	}   
	printf("\n\r");
    }   

}


void i2c_test(void)
{
    char c;

    init_i2c_ctl();
    while (1)
    {
	/* 打印菜单, 供我们选择测试内容 */
	printf("[w] Write at24cxx \n\r");
	printf("[r] Read at24cxx \n\r");
	printf("[q] quit\n\r");
	printf("Enter selection: ");

	c = getchar();
	printf("%c\n\r", c);

	/* 测试内容:
	 * 1. 识别nor flash
	 * 2. 擦除nor flash某个扇区
	 * 3. 编写某个地址
	 * 4. 读某个地址
	 */
	switch (c)		 
	{
	    case 'q':
	    case 'Q':
		return;
		break;
	    case 'w':
	    case 'W':
		do_write_at24cxx();
		break;

	    case 'r':
	    case 'R':
		do_read_at24cxx();
		break;
	    default:
		break;
	}
    }
}

